
#define KeyPin  11     //按键开关引脚定义，红色
#define FkeyPin A5     //频率切换按键，白色

#define pinnumber 6
#define vpnumber 15
// const int tPin[pinnumber][2] = {{2,3},{4,5},{6,7},{8,9},{A2,A1},{A4,A3}};  //1号引脚定义,灰的在前，白的在后

/*

  1     2      3

  6     5      4
*/

const int tPin[pinnumber][2] = {{8,9},{A2,A1},{A4,A3},{2,3},{4,5},{6,7}};  //1号引脚定义,灰的在前，白的在后
/*

  4     5      6

  3     2      1


  1:(1),2:(2),3:(3),4:(4),5:(1,2),6:(2,3),7:(3,4),8:(1,4),9:(1,2,3,4)

*/
const int tvpin[15] = {0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x3, 0x6, 0x24, 0x30, 0x18, 0x9, 0x1b, 0x36, 0x3f};




void cleanAllTouch(){
  for(int i = 0;i<pinnumber;i++){
    digitalWrite(tPin[i][1], LOW);
    digitalWrite(tPin[i][0], HIGH);
  }
}

void touchPinHex(int p){
  if(p&0x01){
    digitalWrite(tPin[0][1], HIGH);
    digitalWrite(tPin[0][0], LOW);  
  }
  if(p&0x02){
    digitalWrite(tPin[1][1], HIGH);
    digitalWrite(tPin[1][0], LOW);  
  }
  if(p&0x04){
    digitalWrite(tPin[2][1], HIGH);
    digitalWrite(tPin[2][0], LOW);  
  }
  if(p&0x08){
    digitalWrite(tPin[3][1], HIGH);
    digitalWrite(tPin[3][0], LOW);  
  }
  if(p&0x10){
    digitalWrite(tPin[4][1], HIGH);
    digitalWrite(tPin[4][0], LOW);  
  }
  if(p&0x20){
    digitalWrite(tPin[5][1], HIGH);
    digitalWrite(tPin[5][0], LOW);  
  }
}

void unTouchPinHex(int p){
  if(p&0x01){
    digitalWrite(tPin[0][0], HIGH);
    digitalWrite(tPin[0][1], LOW);  
  }
  if(p&0x02){
    digitalWrite(tPin[1][0], HIGH);
    digitalWrite(tPin[1][1], LOW);  
  }
  if(p&0x04){
    digitalWrite(tPin[2][0], HIGH);
    digitalWrite(tPin[2][1], LOW);  
  }
  if(p&0x08){
    digitalWrite(tPin[3][0], HIGH);
    digitalWrite(tPin[3][1], LOW);  
  }
  if(p&0x10){
    digitalWrite(tPin[4][0], HIGH);
    digitalWrite(tPin[4][1], LOW);  
  }
  if(p&0x20){
    digitalWrite(tPin[5][0], HIGH);
    digitalWrite(tPin[5][1], LOW);  
  }
}

void touchPin(int p){
  if(p >=0 && p <vpnumber){
    touchPinHex(tvpin[p]);
  }
}
void unTouchPin(int p){
    
if(p >=0 && p <vpnumber){
    unTouchPinHex(tvpin[p]);
  }
}

const int blinkdelay = 50;

void ledBlink(){
  for(int i = 0;i<5;i++){
    digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
    delay(blinkdelay);                       // wait for a second
    digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
    delay(blinkdelay); 
  }
}

void setup() {
  //初始化串口
  Serial.begin(115200);

  //设置随机数种子
  randomSeed(analogRead(A0));
  pinMode(KeyPin, INPUT_PULLUP); //设置按键引脚为输入
  pinMode(FkeyPin, INPUT); //设置按键引脚为输入

  pinMode(LED_BUILTIN, OUTPUT);
  //设置点击头控制引脚为输出
  for(int i = 0;i<pinnumber;i++){
    pinMode(tPin[i][0], OUTPUT);
    pinMode(tPin[i][1], OUTPUT);
  }
  delay(1);
  
  //初始化点击头状态,清除所有当关点击状态
  cleanAllTouch();
  digitalWrite(LED_BUILTIN, LOW);
}

#define MAXFQUNUMBER 2

float fquList[MAXFQUNUMBER] = {7.0,7.9};   //点击频率
int fquIndex = 1;              //当前点击频率编号


int minTime = int(500/fquList[fquIndex]) - 25;  //最小随机间隔时间
int maxTime = int(500/fquList[fquIndex]) + 25;  //最大随机间隔时间

//通过频率更新随机间隔时间
void updateRandomTime(float f){
  minTime = int(500/f) - 25;  //最小随机间隔时间
  maxTime = int(500/f) + 25;  //最大随机间隔时间
}

int ttime = 125;
int utime = 125;
int p = 0;
void loop() {
  int k = digitalRead(KeyPin);
  if(!k){
    
    ttime = 500;
    touchPin(p);
    delay(ttime);
    utime = 500;
    unTouchPin(p);
    delay(utime);
    Serial.print("touch:");
    Serial.println(p+1);
  }else{
    int fk = digitalRead(FkeyPin);
    if(!fk){
      p++;
      
      if(p>=vpnumber){
        p=0;
      }
      Serial.print("n change to:");
      Serial.println(p+1);
      ledBlink();
    }
  }
}
